package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.pojo.User;
import com.tanhua.server.pojo.UserInfo;
import com.tanhua.server.utils.UserThreadLocal;
import com.tanhua.server.vo.Comments;
import com.tanhua.server.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service
public class CommentsService {

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public PageResult queryCommentList(String publishId, Integer page, Integer pageSize) {

        User user = UserThreadLocal.get();

        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);

        PageInfo<Comment> pageInfo = quanZiApi.queryCommentList(publishId, page, pageSize);
        List<Comment> records = pageInfo.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return pageResult;
        }

        HashSet<Long> userIds = new HashSet<>();
        for (Comment record : records) {
            userIds.add(record.getUserId());
        }

        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.in("user_id", userIds);
        List<UserInfo> userInfoList = userInfoService.queryUserInfoList(wrapper);

        ArrayList<Comments> commentsList = new ArrayList<>();
        for (Comment record : records) {
            Comments comments = new Comments();
            comments.setId(record.getId().toHexString());
            comments.setCreateDate(new DateTime(record.getCreated()).toString("yyyy年MM月dd日 HH:mm"));
            comments.setContent(record.getContent());

            for (UserInfo userInfo : userInfoList) {
                if (record.getUserId().longValue() == userInfo.getUserId().longValue()) {
                    comments.setNickname(userInfo.getNickName());
                    comments.setAvatar(userInfo.getLogo());
                    break;
                }
            }

            //是否点赞:1点赞 0未点赞
            String likeCommentKey = "QUANZI_COMMENT_LIKE_USER_" + user.getId() + "_" + comments.getId();
            //判断是否点赞,如果redis中存在likeCommentKey的key,则值一定为1,否则该key就不存在,直接被delete掉了,因此此处可以通过hasKey来做条件判断
            comments.setHasLiked(redisTemplate.hasKey(likeCommentKey) ? 1 : 0);

            //点赞数
            String likeCommentCountKey = "QUANZI_COMMENT_LIKE_" + comments.getId(); //publishId
            String likeCount = redisTemplate.opsForValue().get(likeCommentCountKey);
            //点赞总数,在redis中是通过自增自减进行操作的,即除非该key过期或第一次被访问,否则会一直存在
            if (StringUtils.isNotEmpty(likeCount)) { //当字符串为null或""时,返回false
                comments.setLikeCount(Integer.valueOf(likeCount));
            } else {
                comments.setLikeCount(0);
            }

            commentsList.add(comments);
        }

        pageResult.setItems(commentsList);
        return pageResult;
    }

    public Boolean saveComments(String publishId, String content) {
        User user = UserThreadLocal.get();
        return quanZiApi.saveComment(user.getId(), publishId, 2, content);
    }
}
